<?php
require_once ("secure_area.php");
class Services extends Secure_area
{
	function __construct()
	{
		parent::__construct('services');
		$this->load->library('serv_lib');
		$this->load->library('sale_lib');
		$this->load->library('session');
		$this->load->model('Appconfig');
	}

	function index()
	{
		$this->_reload();
	}
	
	//Load layout to add or edit service
	function view($service_id=-1)
	{
		$service_problem = $this->Appconfig->get('service_problem'); 
		$service_problem = explode(';', $service_problem) ;
		$list_problem = array();
		foreach ($service_problem as $value){
			if(!empty($value)){
				$search  = array('{', '}');
				$replace = array('', '');
				$value = str_replace($search, $replace, $value);
				$list_problem[] = $value;
			}
		}
		$data['service_problem'] = $list_problem;
		$data['customer_info']=$this->Customer->get_list_cus(20,0);
		if($service_id!=-1){
			$data['service_info']=$this->Service->get_info($service_id);
			$data['list_items_add'] = $this->Service->get_service_order_item($service_id);
		}else{
			$data['service_info'] = array();
		}
		
		$customer_id = $this->serv_lib->get_customer();
		if( $customer_id !=-1)
		{
			$data['customer_id'] = $customer_id;
		}else{
			$data['customer_id'] = '';
		}
		
		$this->load->view("services/form",$data);
	}
	
	//Save service
	function save($service_id)
	{
            
		$service_data = array(
			'employee_id' => $this->session->userdata('person_id'),
			'customer_name' => $this->input->post('customer_name'),
			'phone_num' => $this->input->post('phone_num'),
			'product_brand' => $this->input->post('product_brand'),
			'product_model' => $this->input->post('product_model'),
			'service_note' => $this->input->post('product_note'),
			'problem_description' => $this->input->post('product_problem'),
			'service_cost' => $this->input->post('product_cost'),
			'date_return' => $this->convert_date($this->input->post('product_date_return')),
			'date_created' => date('Y-m-d'),
			'print' => '0',
			'status' => ''
		);
		if($service_id!=-1){
			$list_items_add = $this->Service->get_service_order_item($service_id);
			$delete_array = array();
			foreach($list_items_add as $l_i_a){
				$key = $l_i_a['id'];
				if(!empty($key)){
					$item_a = $this->input->post($key);
					if($item_a==''){
						$delete_array[] =  $key;
					}
				}				
			}
		}
		$this->Service->delete_serv_order_item($delete_array);
		$this->session->set_userdata('customer_name',$service_data['customer_name']);
		$this->session->set_userdata('phone_num',$service_data['phone_num']);
		if ($this->Service->update($service_data, $service_id))
		{
			echo json_encode(array('success'=>true,'message'=>lang('services_successfully_updated')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>lang('services_unsuccessfully_updated')));
		}
	}
	
	//Delete service
	function delete_service($service_id)
	{
		$service_data = array('deleted' => 1);
		
		if ($this->Service->update($service_data, $service_id))
		{
			echo json_encode(array('success'=>true,'message'=>lang('services_successfully_updated')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>lang('services_unsuccessfully_updated')));
		}
	}
	
	function print_view($service_id=-1)
	{
		$customer_id = $this->serv_lib->get_customer();
		
		if( $customer_id !=-1)
		{
			$info = $this->Customer->get_info($customer_id);
			$data['customer']=$info->first_name.' '.$info->last_name;
			$data['company'] = ($info->company_name=='') ? '' : $info->company_name ;
			$data['customer_email']=$info->email;
			$data['address1']=$info->address_1;
			$data['address2']=$info->address_2;
			$data['country']=$info->country;
			$data['customer_id'] = $customer_id;
		}
		
		$person_info = $this->Employee->get_logged_in_employee_info();
		
		$data['cart']=$this->Service->get_all($customer_id);
		$data['num_services'] = $this->Service->count_all($customer_id);;
		$data['total_cost'] = $this->Service->total_cost($customer_id);
		
		$this->load->view("services/print_view",$data);
	}
	
	function select_customer()
	{
		$data = array();
		$customer_id = $this->input->post("customer");
		if ($this->Customer->exists($customer_id))
		{
			$this->serv_lib->set_customer($customer_id);
		}
		else
		{
			$data['error']=lang('sales_unable_to_add_customer');
		}
		
		if(!empty($customer_id)){
			$this->Service->update_none_cus($customer_id);
		}
		
		$this->_reload($data);
	}
	
	function complete()
	{
		$temp = $this->session->userdata('list_make_receipt');
		if($temp!=''){
			$this->session->unset_userdata('list_make_receipt');
		}
		$type = $this->input->post('type');
		
		if($type=='print_receipt'){
			$list_serv = $this->input->post('receipt_list');
		}if($type=='print_invoice'){
			$list_serv = $this->input->post('service_list');
		}
		$list_serv = (empty($list_serv)) ? '':$list_serv;
		
		$list_needed = explode(',',$list_serv);
		//var_dump($list_needed);
		//exit();
		foreach ($list_needed as $dt){
			$data['ser_added'.$dt] = $this->Service->get_service_order_item($dt);
		}		
		$person_info = $this->Employee->get_logged_in_employee_info();
		$data['cus_name']=$person_info->first_name.' '.$person_info->last_name;
		$data['cus_phone']=$person_info->phone_number;
		$data['recpt_title']=lang('services_receipt');
		$data['transaction_time']= date(get_date_format().' '.get_time_format());
		
		$data['cart']=$this->Service->get_all_print($list_serv);
		$data['num_services'] = $this->Service->count_all_print($list_serv);;
		$data['total_cost'] = $this->Service->total_cost_print($list_serv);
		$service_term = $this->Appconfig->get('service_term');
		$service_term = explode(';', $service_term) ;
		$list_term = array();
		foreach ($service_term as $value){
			if(!empty($value)){
				$search  = array('{', '}');
				$replace = array('', '');
				$value = str_replace($search, $replace, $value);
				$list_term[] = $value;
			}
		}
		
		$nos = $this->Service->getNos($list_serv);
		foreach ($nos as $no){
			$no_receipt = $no['print'];
		}
		
		$data['service_term'] = $list_term;
		$data['order_title']=(empty($list_serv)) ? 'Receipt ID':'Order ID';
		//$order_id = substr(md5(mktime()),0,5);
		$order_ids = $this->Service->get_max_print();
		foreach ($order_ids as $order_id){
			$order_num = $order_id['print'];
		}
		
		//Update fields printed
		$service_data = array();
			$order_num = ($order_num==0) ? 1 : ($order_num + 1) ;
			$length = strlen($order_num);
			if($length<5){
				for($kk = 0; $kk <(5-$length); $kk++){
					$order_num = (string)'0'.$order_num;			
				}
			}
		if($type=='print_receipt'){
			$data['order_id'] = $order_num;
			$data['no'] = $order_num ;
			$service_data['print'] = $data['order_id'];
			$service_data['status'] = 'Waiting';
			$data['type'] = 'print_receipt';
		}else if($type=='print_invoice'){
			$service_data['status'] = 'Done';
			$data['no'] = $no_receipt ;
		}
		$this->Service->update_print($service_data,$list_serv);
		
		$this->load->view("services/print_view",$data);
	}
	
	function make_receipt()
	{
		$list_serv = $this->input->post('make_receipt_list');
		$this->session->set_userdata('list_make_receipt',$list_serv);
		redirect('sales/index', 'location');
	}
	
	function serv_return()
	{
		$list_serv = $this->input->post('service_list');
		$list_serv = (empty($list_serv)) ? '':$list_serv;
		
		$service_data = array('status'=>'Return');
		
		if($this->Service->update_status($service_data,$list_serv)){
			//echo json_encode(array('success'=>true,'message'=>'status successfully updated'));
			$this->_reload();
		}
		else
		{
			//echo json_encode(array('success'=>false,'message'=>'status unsuccessful updated'));
			$this->_reload();
		}
	}
	
	function Delete()
	{
		$list_serv = $this->input->post('serv_delete_list');
		if ($this->Service->delete($list_serv))
		{
			//echo json_encode(array('success'=>true,'message'=>'Services deleted successfully'));
			$this->_reload();
		}
		else
		{
			//echo json_encode(array('success'=>false,'message'=>'Services deleted unsuccessful'));
			$this->_reload();
		}
		
	}
	
	function _reload($data=array())
	{
		/*
		if( $customer_id !=-1)
		{
			$info = $this->Customer->get_info($customer_id);
			$data['customer']=$info->first_name.' '.$info->last_name.($info->company_name==''  ? '' :' ('.$info->company_name.')');
			$data['customer_email']=$info->email;
			$data['customer_id'] = $customer_id;
		}*/
		
		$person_info = $this->Employee->get_logged_in_employee_info();
		
		$config['base_url'] = site_url('services/index');
		$config['total_rows'] = $this->Service->count_all();
		$config['per_page'] = $this->config->item('number_of_items_per_page') ? (int)$this->config->item('number_of_items_per_page') : 20; 
		$this->pagination->initialize($config);
		
		$data['cart']=$this->Service->get_all($config['per_page'], $this->uri->segment(3),$this);
		//$data['num_services'] = $config['totalrows'];
		$data['total_cost'] = $this->Service->total_cost();
		
		$this->load->view("services/services",$data);
	}
	
	function search()
	{
		$search=$this->input->post('search');
		$person_info = $this->Employee->get_logged_in_employee_info();
		
		$config['base_url'] = site_url('services/index');
		$config['total_rows'] = $this->Service->count_search($search);
		$config['per_page'] = $this->config->item('number_of_items_per_page') ? (int)$this->config->item('number_of_items_per_page') : 20; 
		$this->pagination->initialize($config);
		
		$data['cart']=$this->Service->search($search,$config['per_page'], $this->uri->segment(3),$this);
		//$data['num_services'] = $config['totalrows'];
		$data['total_cost'] = $this->Service->total_cost_search($search);
		
		$this->load->view("services/search",$data);
	}
	function suggest()
	{
		$suggestions = $this->Service->get_search_suggestions($this->input->get('term'),100);
		echo json_encode($suggestions);
	}
	
	function item_search()
	{
		$suggestions = $this->Service->get_item_search_suggestions($this->input->get('term'),100);
		echo json_encode($suggestions);
	}
	
	function get_row()
	{
		$service_id = $this->input->post('row_id');
		$data_row=get_item_data_row($this->Service->get_info($service_id),$this);
		echo $data_row;
	}
}
?>